{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 多线程读取数据版本\n",
    "import requests\n",
    "import time\n",
    "import asyncio\n",
    "import aiohttp\n",
    "import nest_asyncio\n",
    "headers= {\n",
    "'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'\n",
    "}\n",
    "async def download_one(url):\n",
    "    #resp = requests.get(url)\n",
    "    async with aiohttp.ClientSession() as session:\n",
    "        async with session.get(url, headers=headers) as resp:\n",
    "            print('Read {} from {}'.format(resp,url))\n",
    "async def download_all(sites):\n",
    "    tasks = [asyncio.create_task(download_one(site)) for site in sites]\n",
    "    await asyncio.gather(*tasks)\n",
    "    # for site in sites:\n",
    "        \n",
    "            #download_one(site)\n",
    "def main():\n",
    "    sites = [\n",
    "        'https://www.baidu.com/s?wd=周杰伦',\n",
    "        'https://www.baidu.com/s?wd=周饼伦',\n",
    "        'https://www.baidu.com/s?wd=周口伦',\n",
    "        'https://www.baidu.com/s?wd=黑伦',\n",
    "        'https://www.baidu.com/s?wd=粥饼伦',\n",
    "        'https://www.baidu.com/s?wd=雍杰伦',\n",
    "        'https://www.baidu.com/s?wd=孙冬',\n",
    "        'https://www.baidu.com/s?wd=周口同学',\n",
    "        'https://www.baidu.com/s?wd=上海杰伦老师',\n",
    "        'https://www.baidu.com/s?wd=方文山',\n",
    "        'https://www.baidu.com/s?wd=马云',\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    nest_asyncio.apply()\n",
    "    asyncio.run(download_all(sites))  # 改用asyncio.run()函数代替asyncio.get_event_loop().run_until_complete()\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 串行执行与并发执行，多进程例子\n",
    "import time\n",
    "import concurrent.futures\n",
    "import multiprocessing\n",
    "def cpu_bound(number):\n",
    "    print(sum(i * i for i in range(number)))\n",
    "\n",
    "def calculate_sums(numbers):\n",
    "    for number in numbers:\n",
    "        cpu_bound(number)\n",
    "def mutil_process_sums(numbers):\n",
    "    # with concurrent.futures.ProcessPoolExecutor() as executor:\n",
    "    #     executor.map(cpu_bound, numbers)\n",
    "    with multiprocessing.Pool() as pool:\n",
    "        pool.map(cpu_bound, numbers)\n",
    "def main():\n",
    "    #start_time = time.perf_counter()  \n",
    "    numbers = [10000000 + x for x in range(20)]\n",
    "    # end_time = time.perf_counter()\n",
    "    # calculate_sums(numbers)\n",
    "    # print('串行执行了：{}秒'.format(end_time - start_time))\n",
    "    start_time = time.perf_counter()  \n",
    "    end_time = time.perf_counter()\n",
    "    mutil_process_sums(numbers)\n",
    "    print('多进程执行了：{}秒'.format(end_time - start_time))\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
